using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CloudApiServer.Controllers
{
    [RoutePrefix("html")]
    public class HtmlController : ApiController
    {
        [HttpGet]
        public HttpResponseMessage GetPage(string page)
        {
            // 设置默认页面
            if (string.IsNullOrEmpty(page))
            {
                page = "index.html";
            }

            // 构建文件路径
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "html", page);

            // 检查文件是否存在
            if (!File.Exists(filePath))
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            // 读取文件内容
            string fileContent = File.ReadAllText(filePath);

            // 获取文件扩展名
            string fileExtension = Path.GetExtension(filePath).ToLower();

            // 设置内容类型
            string contentType = "text/html";
            switch (fileExtension)
            {
                case ".html":
                    contentType = "text/html";
                    break;
                case ".css":
                    contentType = "text/css";
                    break;
                case ".js":
                    contentType = "application/javascript";
                    break;
                case ".png":
                    contentType = "image/png";
                    break;
                case ".jpg":
                case ".jpeg":
                    contentType = "image/jpeg";
                    break;
                case ".gif":
                    contentType = "image/gif";
                    break;
                default:
                    contentType = "application/octet-stream";
                    break;
            }
            
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(fileContent, System.Text.Encoding.UTF8, contentType)
            };

            return response;
        }
    }
}
